package com.heima.article.listener;


import com.alibaba.fastjson.JSON;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.common.constants.ReadBehaviorConstants;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.behavior.dtos.Behavior;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class KafkaReadbBehaviorListener {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @KafkaListener(topics = ReadBehaviorConstants.BEHAVIOR_TABLE_NAME)
    public void updateArticleBehavior(String message) {
        log.info("接收到消息:{}",message);
        // 解析消息
        Behavior behavior = JSON.parseObject(message, Behavior.class);

        ApArticle apArticle = new ApArticle();
        // 更新阅读数+1
        apArticle.setViews(behavior.getCount());
        apArticle.setId(behavior.getArticleId());
        apArticleMapper.updateById(apArticle);
    }
}
